export class treeNode {
  Tag!: string;
  Text!: string;
  Children!: Array<treeNode>;
  Parent!: treeNode;
  SerialNo!: string;
  Level!: number;
  Deleted!: boolean;
  Index: number = -1;
  Expand: boolean = false;

  /**
   * 展开节点以及所有父节点
   */
  public ExpandNode(node: treeNode = this): void {
    if (node.Parent) {
      this.ExpandNode(node.Parent);
    }
    node.Expand = true;
  }

  /**
   * tree 目标数组  node 当前节点  折叠除当前节点枝叶外的所有节点
   */
  public Collapse(tree: treeNode[]): void {
    var node = this;
    var roots: treeNode[] = [];
    this.GetRoots(node, roots);

    tree.forEach(n => {
      //未找到
      if (roots.indexOf(n) < 0) {
        n.Expand = false;
        if (n.Children && n.Children.length > 0) {
          this.Collapse(n.Children);
        }
      }
    });
  }

  public GetRoots(node: treeNode, nodes: treeNode[] = []) {
    if (node.Parent) {
      this.GetRoots(node.Parent, nodes);
      nodes.push(node.Parent);
    }
    nodes.push(node);
  }
}
